<?php

namespace app\admin\model\user;

use app\common\model\Category;
use app\common\model\User;
use think\Model;
use traits\model\SoftDelete;

class Artificer extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'artificer';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'vehicle_type_text',
        'pay_status_text',
        'pay_way_text',
        'state_text',
        'verifytime_text',
        'paytime_text',
        'refund_status_text',
        'refundtime_text',
        'category_texts',
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getVehicleTypeList()
    {
        return ['10' => __('Vehicle_type 10'), '20' => __('Vehicle_type 20')];
    }

    public function getPayStatusList()
    {
        return ['10' => __('Pay_status 10'), '20' => __('Pay_status 20')];
    }

    public function getPayWayList()
    {
        return ['10' => __('Pay_way 10'), '20' => __('Pay_way 20'), '30' => __('Pay_way 30')];
    }

    public function getStateList()
    {
        return ['10' => __('State 10'), '20' => __('State 20'), '30' => __('State 30')];
    }

    public function getRefundStatusList()
    {
        return ['10' => __('Refund_status 10'), '20' => __('Refund_status 20')];
    }


    public function getVehicleTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['vehicle_type']) ? $data['vehicle_type'] : '');
        $list = $this->getVehicleTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayWayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_way']) ? $data['pay_way'] : '');
        $list = $this->getPayWayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVerifytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['verifytime']) ? $data['verifytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refundtime']) ? $data['refundtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setVerifytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRefundtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getCategoryTextsAttr($value, $data)
    {
        $value = '';
        if(isset($data['category_ids']) && $data['category_ids']){
            $temp = Category::where('id','in',$data['category_ids'])->column('name');
            $value = $temp ? implode(',',$temp) : '';
        }
        return $value;
    }

    public function user()
    {
        return $this->belongsTo(User::class )->setEagerlyType(0);
    }


}
